$(function () {
    window.emojiPicker = new EmojiPicker({
        emojiable_selector: '[data-emojiable=true]',
        assetsPath: '/addons/emoji-picker/lib/img',
        popupButtonClasses: 'fa fa-smile-o'
    });
    window.emojiPicker.discover();
});

$(document).on('click', '.comment-response', function () {
    var num = $(this).closest('.cimo-comment-right').attr('data-commentid');
    var uid = $(this).attr('data-uid');
    $('#commentform input[name=to_userid]').val(uid);
    $('#commentform input[name=cmtid]').val(num);
    if ($(this).attr('data-uname')) {
        var toname = '回复' + $(this).attr('data-uname');
        $('#commentform span.toname').html(toname);
    }
    if (!$(this).hasClass('click')) {
        $('.comment-response').removeClass('click');
        $(this).addClass('click');
        $('#commentform').show();
        $('#commentform').insertAfter($('#comment-reply' + num));
    } else {
        var ishidden = $('#commentform').css('display');
        if (ishidden == "none") {
            $('#commentform').show();
        } else {
            $('#commentform').hide();
        }
    }
});

//点赞
$(document).on('click', '.comment-thumb', function () {
    var value = $(this).attr('data-id');
    var nn = parseInt($(this).next('span').html());
    var nh = $(this).next('span');
    var type = $('input[name=comment_typeid]').val();
    $.ajax({
        type: 'post',
        cache: 'false',
        url: '/comment/thumb?cid=' + value,
        data: 'typeid=' + type,
        dataType: 'json',
        success: function (data) {
            if (data.status == 'success') {
                var jsonData = data.data;
                var html = '';
                if (data.total == 1) {
                    layer.msg('点赞成功');
                } else if (data.total > 1) {
                    nn += 1;
                    nh.html(nn);
                    layer.msg(data.msg);
                }
            } else {
                layer.msg(data.msg);
            }
        },
        error: function () {
            layer.msg('未知原因,点赞失败');
        }
    });
});

//一级回复
$(document).on('click', '#submit-main', function () {
    var comment_typeid = $('input[name=comment_typeid]').val();
    var url = '/comment/push?typeid=' + comment_typeid;
    var comentapp = $('input[name=commentapp]').val();
    var comentappid = $('input[name=commentappid]').val();
    var token = $('input[name=_token]').val();
    cimo_first_comment(url, comentapp, comentappid, token);
});

//举报
$(document).on('click', '.comment-tipoff', function () {
    var tid = $(this).attr('data-id');
    $('input[name=tipoffid]').val(tid);
    $('#examModalcm').modal();
});

$(document).on('click', '.comment-tipoffs', function () {
    var value = $('input[name=tipoffid]').val();
    var reason = $('select[name=reason]').val();
    var type = $('input[name=comment_typeid]').val();
    $('#examModalcm').modal('hide');
    $.ajax({
        type: 'post',
        cache: 'false',
        url: '/comment/tipoff?cid=' + value + '&reason=' + reason,
        data: 'typeid=' + type,
        dataType: 'json',
        success: function (data) {
            if (data.status == 'success') {
                var jsonData = data.data;
                var html = '';
                if (data.total == 1) {
                    layer.msg('举报成功，谢谢');
                } else if (data.total > 1) {
                    layer.msg(data.msg);
                }
            } else {
                layer.msg(data.msg);
            }
        },
        error: function () {
            layer.msg('举报成功，谢谢');
        }
    });
});

$(document).on('click', '.secondsubmit', function () {
    var comment_typeid = $('input[name=comment_typeid]').val();
    var url = '/comment/push?typeid=' + comment_typeid;
    var cmtid = $('input[name=cmtid]').val();
    var cuid = $('input[name=to_userid]').val();
    var comentapp = $('input[name=commentapp]').val();
    var comentappid = $('input[name=commentappid]').val();
    var token = $('input[name=_token]').val();
    cimo_second_comment(url, cmtid, cuid, comentapp, comentappid, token);

});
//二级评论
function cimo_second_comment(url, comid, uid, app, appid, token) {
    var content = $(".comment-reply-content-common").val();
    $.ajax({
        type: 'post',
        cache: 'false',
        url: url,
        data: 'cid=' + comid + '&content=' + content + '&touid=' + uid + '&app=' + app + '&appid=' + appid + '&_token=' + token,
        dataType: 'json',
        success: function (data) {
            if (data.status == 'success') {
                var jsonData = data.data;
                var html = '';
                if (data.total == 1) {
                    html += '<div class="comment-caret-up"><i class="glyphicon glyphicon-triangle-top"></i></div>';
                    html += '<div class="comment-child-content" id="comment-child-content' + comid + '">';
                    html += '<div class="comment-child-content-list">';
                    html += '<div class="color-green"><span  style="color: #383838;">'
                        + jsonData.username
                        + '</span><span style="margin: 0 4px; color:#999;">回复</span><span style="color: #383838;">'
                        + jsonData.tousername
                        + '</span>'
                        + '<span style="float:right;" class="childrig">'
                        // + '<a class="comment-tipoff" data-id="' + jsonData.comment_id + '">举报</a>'
                        + '<a class="comment-thumb" data-id="' + jsonData.comment_id + '"></a><span>0</span>'

                        + '</span>'
                        + '</div>';
                    /*   + '<span class="cimo-comment-time">'
                     + jsonData.created_at + '</span>'*/
                    html += '<p class="comment-child-cont" >' + jsonData.content + '</p>';
                    html += '</div>';
                    html += '</div>';
                    $("#comment-childs" + comid).append(html);
                } else if (data.total > 1) {
                    html += '<div class="comment-child-content-list" style="">';
                    html += '<div class="color-green"><span  style="color: #383838;">'
                        + jsonData.username
                        + '</span><span style="margin: 0 4px; color:#999;">回复</span><span style="color: #383838;">'
                        + jsonData.tousername
                        + '</span>'
                        + '<span style="float:right;" class="childrig">'
                        // + '<a class="comment-tipoff" data-id="' + jsonData.comment_id + '">举报</a>'
                        + '<a class="comment-thumb" data-id="' + jsonData.comment_id + '"></a><span>0</span>'
                        + '</span>'
                        + '</div>';
                    /*    + '<span class="cimo-comment-time">'
                     + jsonData.created_at + '</span>'*/
                    html += '<p class="comment-child-cont">' + jsonData.content + '</p>';
                    html += '</div>';
                    $("#comment-child-content" + comid).append(html);
                }
                $("#message" + comid).show().html("发表成功！").fadeOut(2000);
                $("#comment-reply" + comid).hide();
                $(".comment-reply-content-common").val("");
                $(".comment-reply-content-common").next('div').html("");

                $("span[name=totalnum" + comid + "]").html(data.total);

                $("#comment-reply-content" + comid).val("");
            } else {
                $("#message" + comid).show().html(data.msg).fadeOut(2000);
            }
        },
        error: function () {
            $("#message" + comid).show().html("发表失败！").fadeOut(2000);
        }
    });
}


//一级评论
function cimo_first_comment(url, app, appid, token) {
    var content = $("#comment-reply-content").val();
    $.ajax({
        type: 'post',
        cache: 'false',
        url: url,
        data: 'content=' + content + '&app=' + app + '&appid=' + appid + '&_token=' + token,
        dataType: 'json',
        success: function (data) {
            if (data.status == 'success') {
                var jsonData = data.data;
                var html = '';
                html += '<div class="cimo-comment-common" >';
                html += '<div class="cimo-comment-user-avatar fl ">';
                html += '<img src="' + jsonData.logo + '">';
                html += '</div>';
                html += '<div class="fl cimo-comment-right" data-commentid="' + jsonData.comment_id + '">';
                html += '<div class="comment-content text-left" style="">';
                html += '<div class="cimo-comment-first"><span class="cimo-comment-username">' + jsonData.username + '</span>'
                    + '<span style="float:right;" class="clearfix">'
                    + '<a href="javascript:;" class="comment-response" data-uid="' + jsonData.user_id + '"></a>'
                    + '<span name="totalnum' + jsonData.comment_id + '">0</span>'
                    + '<a class="comment-thumb" data-id="' + jsonData.comment_id + '"></a><span>0</span>'
                    + '<a class="comment-tipoff" data-id="' + jsonData.comment_id + '"></a></span>'
                    + '</div>';
                html += '<div class="fs14 c9 ma0">';
                html += '<div class="clr"></div>';
                html += '<p class="cimo-comment-second" >' + jsonData.content + '</p>';
                html += '</div>';
                html += '</div>';
                html += '<div class="comment-child text-left" id="comment-childs' + jsonData.comment_id + '">';
                html += '</div>';
                html += '<div id="message' + jsonData.comment_id + '" class="commentmsg dn"></div>';
                html += '<div id="comment-reply' + jsonData.comment_id + '" class="comment-reply-form" style=" border: none;"></div>';
                html += '</div>';
                html += '<div class="clr"></div>';
                html += '</div>';
                $("#cimo-comment-container").prepend(html);
                $("#message").show().html("发表成功！").fadeOut(2000);
                $("#comment-reply-content").val("");
                $("#comment-reply-content").next('div').html("");
                $('#holder').show();
                jPages();
            } else {
                $("#message").show().html(data.msg).fadeOut(5000);
            }
        },
        error: function () {
            $("#message").show().html("发表失败！").fadeOut(2000);
        }
    });
}


function cjPages() {
    $("div.holder").jPages({
        containerID: "cimo-comment-container",
        previous: "上一页",
        next: "下一页",
        perPage: 4,
        animation: 'fadeInRight'
    });
    var loc = window.location.hash;
    if (loc != '' && loc != undefined) {
        loc = parseInt(loc.substr(1));
        var locpage = $('a[name=' + loc + ']').attr('cdata-id');
        locpage = parseInt(locpage);
        locpage = Math.ceil(locpage / 4);
        $(".holder").jPages(locpage);
    }
}